// pages/policy/policy.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    optionView: false,
    selList: [],
    count: 0,
    departmentList: [],
    industryList: [],
    scaleList:[],
    keyword: "",
  },

  //选择规模
  scaleClick:function(e){
    var val = e.currentTarget.dataset.index;
    var scaleList = this.data.scaleList;
    scaleList[val].active = !scaleList[val].active;
    this.setData({
      scaleList:scaleList
    })
  },

  //选择发布部门
  departmentClick: function (e) {
    var val = e.currentTarget.dataset.index;
    var departmentList = this.data.departmentList;
    departmentList[val].active = !departmentList[val].active;
    this.setData({
      departmentList:departmentList
    })
  },

  //选择行业
  industryClick: function (e) {
    var val = e.currentTarget.dataset.index;
    var industryList = this.data.industryList;
    industryList[val].active = !industryList[val].active;
    this.setData({
      industryList:industryList
    })
  },

  //输入关键字
  keywordClick: function (e) {
    this.setData({
      keyword: e.detail.value
    });
  },

  //获取总数据
  countApi: function () {
    var params = {};
    //获取项目列表
    app.request('post', '/api/Policy/countApi', params).then(res => {
      this.setData({
        count: res.data.count,
        departmentList: res.data.departmentList,
        industryList: res.data.industryList,
        scaleList:res.data.scaleList
      });
    }).catch(err => console.log(err));

  },

  optionClick: function () {
    this.setData({
      optionView: !this.optionView
    });
  },

  //点击搜索按钮
  searachClick: function () {
    var keyword = this.data.keyword;
    var selList = this.data.selList;
    selList= JSON.stringify(selList);
    wx.navigateTo({ url: `/pages/policy/policyList?keyword=${keyword}&selList=${selList}` })
  },

  //删除单个条件
  delOption: function (e) {
    var selList = this.data.selList;
    var val = e.currentTarget.dataset.index;
    for (var i = 0; i < selList.length; i++) {
      if (i == val) {
        selList.splice(i, 1);
      }
    }
    this.setData({ selList: selList });
  },

  //确认操作
  confirmClick:function(){
    var departmentList = this.data.departmentList;
    var industryList = this.data.industryList;
    var scaleList = this.data.scaleList;
    var selList = [];
    departmentList.forEach(function(item,key){
      if(item.active==true){
        selList.push(item);
      }
    });
    industryList.forEach(function(item,key){
      if(item.active==true){
        selList.push(item);
      }
    });
    scaleList.forEach(function(item,key){
      if(item.active==true){
        selList.push(item);
      }
    });
    this.setData({
      selList:selList,
      optionView: false
    });
  },

  //重置操作
  resetClick: function () {
    var departmentList = this.data.departmentList;
    var industryList = this.data.industryList;
    var scaleList = this.data.scaleList;
    departmentList.forEach(function(item,key){
      departmentList[key].active = false;
    });
    industryList.forEach(function(item,key){
      industryList[key].active = false;
    });
    scaleList.forEach(function(item,key){
      scaleList[key].active = false;
    });
    
    this.setData({
      optionView: false,
      departmentList:departmentList,
      industryList:industryList,
      scaleList:scaleList,
      selList:[]
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.countApi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})